package com.moly.virgo.util;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class FileUtil {


    public static void main(String[] args) {


        getErrorFile("D:\\moly\\download\\ossimport-2.3.4\\ossimport-2.3.4\\logs\\import.log",
                "lost.txt");
    }

    static String testReg(String str) {
        Pattern compile = Pattern.compile(".*key:(.*) Exception");
        Matcher matcher = compile.matcher(str);
        String result = matcher.find() ? matcher.group(1) : "";
        return result;

    }

    static void getErrorFile(String readFile, String writeFile) {
        String fileNameOfRead = readFile;
        String fileNameOfWrite = writeFile;
        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(new File(fileNameOfRead)));
            bufferedWriter = new BufferedWriter(new FileWriter(new File(fileNameOfWrite)));
            String s = bufferedReader.readLine();
            String result = testReg(s);
            if (result != "") {
                bufferedWriter.write(result);
                bufferedWriter.flush();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

        }
    }
}
